﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace ExtenDev.Web.Extensions
{
    public class ControlMethodHandler : ControlMethodImpl, IHttpHandler, IRequiresSessionState
    {
        private readonly IHttpHandler _originalHandler;

        public ControlMethodHandler() { }

        public ControlMethodHandler(IHttpHandler originalHandler)
        {
            _originalHandler = originalHandler;
        }

        public virtual void ProcessRequest(HttpContext context)
        {
            if (!ProcessRestRequest(context.ApplicationInstance))
            {
                if (_originalHandler == null)
                {
                    throw new HttpException(404, "File Not Found");
                }
                else
                {
                    _originalHandler.ProcessRequest(context);
                }
            }
        }

        public bool IsReusable
        {
            get { return false; }
        }

        public IHttpHandler OriginalHandler
        {
            get
            {
                return _originalHandler;
            }
        }
    }
}
